package com.jkc.xh.adapters;

import android.content.Intent;
import android.support.v4.view.PagerAdapter;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.jkc.xh.activities.WebActivity_;
import com.jkc.xh.constants.Constant;
import com.jkc.xh.models.AdImageModel;
import com.jkc.xh.utils.ImageLoader;

import java.util.ArrayList;

/**
 * Created by zhangbreeze on 15/11/20.
 */
public class LogoViewPageAdapter extends PagerAdapter {

    private ArrayList<AdImageModel> images = new ArrayList<AdImageModel>();


    @Override
    public int getCount() {
        return images.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }


    public int getItemPosition(Object object) {
        return POSITION_NONE;
    }


    public Object instantiateItem(ViewGroup container, final int position) {
        final ImageView view = new ImageView(container.getContext());
        view.setScaleType(ImageView.ScaleType.FIT_XY);
        AdImageModel image = images.get(position);
        if (!TextUtils.isEmpty(image.img)) {
            ImageLoader.loadImage(image.img, view);
        }
        container.addView(view);

        if (!TextUtils.isEmpty(image.link)) {
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(view.getContext(), WebActivity_.class);
                    intent.putExtra(Constant.ACTION_AD_URL, images.get(position).link);
                    view.getContext().startActivity(intent);
                }
            });
        }
        return view;
    }

    public void setData(ArrayList<AdImageModel> images) {
        if (images == null) {
            return;
        }
        this.images.clear();
        this.images.addAll(images);
        notifyDataSetChanged();
    }
}
